package com.atguigu.lease.web.admin.service.impl;


import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 48138
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2025-07-05 14:18:49
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;

    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;

    @Autowired
    private CityInfoMapper cityInfoMapper;

    @Autowired
    private DistrictInfoMapper districtInfoMapper;



    @Override
    public void saveByApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        Date createDate = new Date();
        //1.保存主表
        apartmentInfoMapper.saveByApartmentSubmitVo((ApartmentInfo) getProvinceAndCityAndDistrictInfo(apartmentSubmitVo));
        System.out.println("===============================================");
        System.out.println(apartmentSubmitVo.getIsDeleted());
        System.out.println("===============================================");
        //2.保存从表
        saveOtherTables(apartmentSubmitVo);
    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page,queryVo);
    }

    @Override
    public void saveOtherTables(ApartmentSubmitVo apartmentSubmitVo) {
        Date createDate = new Date();
        ApartmentInfo apartmentInfo = apartmentSubmitVo;
        //2.存到apartmentfacilityInfomapper
        apartmentFacilityMapper.saveByApartmentSubmitVo(apartmentInfo.getId(),apartmentSubmitVo.getFacilityInfoIds(),createDate);
        //3.存到apartmentlabelmapper
        apartmentLabelMapper.saveByApartmentSubmitVo(apartmentInfo.getId(),apartmentSubmitVo.getLabelIds(),createDate);
        //4.存到apartmentfeeValuemapper
        apartmentFeeValueMapper.saveByApartmentSubmitVo(apartmentInfo.getId(),apartmentSubmitVo.getFeeValueIds(),createDate);
        //5.存到apartmentgraphmapper
        graphInfoMapper.saveByApartmentSubmitVo(apartmentInfo.getId(),apartmentSubmitVo.getGraphVoList(),createDate);

    }

    @Override
    public ApartmentSubmitVo getProvinceAndCityAndDistrictInfo(ApartmentSubmitVo apartmentSubmitVo) {
        //2.1查询省份信息
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        //2.2根据查询城市信息
        LambdaQueryWrapper<CityInfo> cityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cityInfoLambdaQueryWrapper.eq(CityInfo::getId,apartmentSubmitVo.getCityId());
        cityInfoLambdaQueryWrapper.eq(CityInfo::getProvinceId,apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoMapper.selectOne(cityInfoLambdaQueryWrapper);
        //3.查询区域信息
        LambdaQueryWrapper<DistrictInfo> districtInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        districtInfoLambdaQueryWrapper.eq(DistrictInfo::getCityId,apartmentSubmitVo.getCityId());
        districtInfoLambdaQueryWrapper.eq(DistrictInfo::getId,apartmentSubmitVo.getDistrictId());
        DistrictInfo districtInfo = districtInfoMapper.selectOne(districtInfoLambdaQueryWrapper);
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());
        return apartmentSubmitVo;
    }

    @Override
    public void removeByIdAndOther(Long id) {
        //1.删除主表
        apartmentInfoMapper.deleteById(id);
        //2.删除从表
        deleteOtherTablesByIds(id);
    }

    @Override
    public void updateStatus(Long id, ReleaseStatus status) {
        apartmentInfoMapper.updateById(id,status);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVo(Long id) {
        return apartmentInfoMapper.getApartmentDetailVo(id);
    }

    @Override
    public void updateByApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
//1更新是主表更新，从表做删除和添加
        //1.1获取省份城市信息
        apartmentSubmitVo = getProvinceAndCityAndDistrictInfo(apartmentSubmitVo);
        //1.2更新主表
//        apartmentInfoMapper
        apartmentInfoMapper.updateById(apartmentSubmitVo);
        //34删除和添加的apartmentfacilityInfomapper，apartmentlabelmapper,apartmentfeeValuemapper,apartmentgraphmapper
        deleteOtherTablesByIds(apartmentSubmitVo.getId());
        saveOtherTables(apartmentSubmitVo);
    }

    @Override
    public void deleteOtherTablesByIds(Long id) {
        Date date = new Date();
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityMapper.delete(apartmentFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelMapper.delete(apartmentLabelLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueMapper.delete(apartmentFeeValueLambdaQueryWrapper);

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        //1代表公寓
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,1);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);
    }
}




